﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DecisionTree
{
    static class Extensions
    {
        public static double StandardDeviation(this List<int> distances)
        {
            double rVal = 0;
            double average = distances.Average();
            foreach (var item in distances)
            {
                rVal += Math.Pow(average - item, 2);
            }
            return Math.Sqrt(rVal / distances.Count);
        }

        public static byte Average(this byte[] distances)
        {
            double sum = 0;
            for (int i = 0; i < distances.Length; i++)
            {
                sum += distances[i];
            }
            double average = sum/distances.Length;
            return Convert.ToByte(average);
        }

        public static byte StandardDeviation(this byte[] distances)
        {
            double rVal = 0;
            byte average = distances.Average();
            for(int i=0;i<distances.Length;i++)
            {
                rVal += Math.Pow(average - distances[i], 2);
            }
            return Convert.ToByte(Math.Sqrt(rVal / distances.Length));
        }

        public static int MaxIndex(this byte[] sequence)
        {
            int maxIndex = 0;
            byte maxValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] > maxValue)
                {
                    maxValue = sequence[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }

        public static int MaxIndex(this double[] sequence)
        {
            int maxIndex = 0;
            double maxValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] > maxValue)
                {
                    maxValue = sequence[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }

        public static int MaxIndex(this int[] sequence)
        {
            int maxIndex = 0;
            int maxValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] > maxValue)
                {
                    maxValue = sequence[i];
                    maxIndex = i;
                }
            }

            return maxIndex;
        }

        public static int MinIndex(this double[] sequence)
        {
            int minIndex = 0;
            double minValue = sequence[0];

            for (int i = 1; i < sequence.Length; i++)
            {
                if (sequence[i] < minValue)
                {
                    minValue= sequence[i];
                    minIndex = i;
                }
            }

            return minIndex;
        }

        public static void AddTo(this double[] vector, double[] vector2)
        {
            for (int i = 0; i < vector.Length; i++)
            {
                vector[i] = vector[i] + vector2[i];
            }
        }
    }
}
